package queue;

import java.util.Arrays;

/**
 * create by mwh 2021-12-28
 *
 * 普通队列的出队和入队操作
 *
 **/

public class QueueCreate {
    /**
     * 队列头
     */
    private int font;
    /**
     *  队列尾
     */
    private int rear;
    /**
     *  数组
     */
    private Object[] objs;
    /**
     *  队列大小
     */
    private static final int DEFAULT_CAPACTITY = 5;
    private static final int MAX_CAPACTITY = Integer.MAX_VALUE - 8;;


    public QueueCreate() {
        objs  =  new Object[DEFAULT_CAPACTITY];
    }

    //入队
    public Object[] enQueue(Object indata) throws Exception {
        if(rear == objs.length){
            //满了
            System.out.println("已经满了,需要扩容，leng+leng/2");
            if(objs.length>MAX_CAPACTITY){
                throw new Exception();
            }
            objs = Arrays.copyOf(objs,objs.length+(objs.length>>1));

        }else{
            objs[rear++] = indata;
        }
        return objs;
    }
    //出队
    public  void deQueue(){
        if(font == rear ){
            //空的
            System.out.println("队列已空");
        }else{
            objs[font++] = null;
        }
    }

    public static void main(String[] args) throws Exception {
      QueueCreate queueCreate = new QueueCreate();
        Object[] objs = queueCreate.objs;
        objs = queueCreate.enQueue("a");
        objs = queueCreate.enQueue("b");
        objs = queueCreate.enQueue("c");
        objs = queueCreate.enQueue("d");
        objs = queueCreate.enQueue("e");
        objs = queueCreate.enQueue("f");
        objs = queueCreate.enQueue("g");
        objs = queueCreate.enQueue("h");
      //插入了5个元素,从队尾插入5个元素
      Arrays.stream(objs).forEach(System.out::println);
      //出队2个元素，从队头出2个元素
      queueCreate.deQueue();
      queueCreate.deQueue();
      Arrays.stream(objs).forEach((t)->{
          System.out.println("出队后元素："+t);
      });
     }

}
